package com.zgs.mall.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.zgs.common.exception.BizCodeEnum;
import com.zgs.mall.member.exception.PhoneException;
import com.zgs.mall.member.exception.UserNameException;
import com.zgs.mall.member.feign.CouponFeignService;
import com.zgs.mall.member.vo.MemberVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.zgs.mall.member.entity.MemberEntity;
import com.zgs.mall.member.service.MemberService;
import com.zgs.common.utils.PageUtils;
import com.zgs.common.utils.R;



/**
 * 会员
 *
 * @author zgs
 * @email 
 * @date 2020-07-12 14:58:26
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;
    @Autowired
    private CouponFeignService couponFeignService;
    @RequestMapping("test")
    public R test(){
        R membercoupons = couponFeignService.membercoupons();
        return membercoupons;
    }

    @RequestMapping("seataAddtest")
    public R seataAddtest(){

        memberService.seataAddtest();
        return R.ok();
    }




    //登录
    @PostMapping("/login")
    public R login(@RequestBody  MemberVo vo){
        MemberEntity entity =memberService.login(vo);
        if (entity==null){
            return R.error(BizCodeEnum.LOGINACCT_PASSWORD_EXCEPTION.getCode(),BizCodeEnum.LOGINACCT_PASSWORD_EXCEPTION.getMsg());
        }
        return R.ok().setData(entity);
    }
    //注册会员
    @PostMapping("regist")
    public R regist(@RequestBody  MemberVo vo){
        try{
            memberService.regist(vo);
        }catch (PhoneException e){
          return  R.error();
        }catch (UserNameException e){

        }

        return R.ok();
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
   // @RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
   // @RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
  //  @RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
  //  @RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
